package com.situ.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.pojo.Product;
import com.situ.mall.service.IProductService;
import com.situ.mall.util.LayUITableResult;
import com.situ.mall.util.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public LayUITableResult selectByPage(Integer page, Integer limit) {
        System.out.println("ProductServiceImpl.selectByPage");
        PageHelper.startPage(page, limit);
        List<Product> list = productMapper.selectAll();
        PageInfo pageInfo = new PageInfo(list);
        long total = pageInfo.getTotal();

        return LayUITableResult.ok(total, list);
    }

    @Override
    public void insert(Product product) {
        productMapper.insert(product);

        // 将上传的图片保存到Redis中
        redisTemplate.opsForSet().add(RedisConstant.UPLOAD_IMAGE_TO_DB, product.getMainImage());
        System.out.println("上传图片到数据库：" + product.getMainImage());
    }
}
